/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import pojo.Scheduledata;

/**
 *
 * @author javauser
 */
public class ScheduleDataDao 
{
    Connection con;

    public ScheduleDataDao(Connection con) 
    {
        this.con = con;
    }
    
///////////////////////////////////////Add New Schedule///////////////////////////////
    public void add(Scheduledata schdata)
    {
        try
        {
            PreparedStatement pstmt = con.prepareStatement("insert into scheduledata(schedule,modifier,start_time,day) values(?,?,?,?)") ;
            pstmt.setString(1,schdata.getSchedule());
            pstmt.setString(2,schdata.getModifier());
            pstmt.setDate(2,schdata.getStartDate());
            pstmt.setString(2,schdata.getStartTime());
            pstmt.setString(2,schdata.getDay());
            pstmt.execute() ;
        }
        catch(SQLException ex1)
        {
            ex1.printStackTrace();
        }
    }
////////////////////////////////////////Delete Schedule///////////////////////////
    public void delete(Scheduledata schdata)
    {
        try
        {
            PreparedStatement pstmt = con.prepareStatement("delete from scheduledata where sd_id=?") ;
            pstmt.setInt(1,schdata.getScId());
            pstmt.execute() ;
        }
        catch(SQLException ex1)
        {
            ex1.printStackTrace();
        }
    }
//////////////////////////////////////Update Schedule////////////////////////////////
    public void update(Scheduledata schdata)
    {
        try 
        {
            boolean first=false;
            String updatetxt="";
            if(schdata.getSchedule()!=null)
            {   first=true;
                updatetxt="schedule='"+schdata.getSchedule()+"'";
            }
            if(schdata.getModifier()!=null)
            {
                if (first) {
                    updatetxt+=", ";
                }
                 first=true;
                updatetxt+="modifier='"+schdata.getModifier()+"'";
            }
            if(schdata.getStartDate()!=null)
            {
                  if (first) {
                    updatetxt+=", ";
                }
                   first=true;
                updatetxt+="start_date='"+schdata.getStartDate()+"'";
            }
            if(schdata.getStartTime()!=null)
            {
                  if (first) {
                    updatetxt+=", ";
                }
                   first=true;
                updatetxt+="start_time='"+schdata.getStartTime()+"'";
            }
            if(schdata.getDay()!=null)
            {
                
                  if (first) {
                    updatetxt+=", ";
                }
                   first=true;
                updatetxt+="dy='"+schdata.getDay()+"'";
            }
        
            PreparedStatement ps=con.prepareStatement("update scheduledata set "+updatetxt+" where sd_id=?");
            ps.setInt(1, schdata.getScId());
            ps.executeUpdate();
            
        } 
        catch (SQLException ex) 
        {
            ex.printStackTrace();
        }
    
    }
    
}
